package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.miaodong.entity.AppIntimacyGuard;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Repository
public class AppIntimacyGuardDao extends BaseDao<AppIntimacyGuard> {

    public AppIntimacyGuard getIntimacyGuard(Integer userId,Integer beiUserId)
    {
        ArrayList<Object> list = new ArrayList<>();
//        String sql = "select * from app_intimacy_guard where (userId=? and beiUserId=?) or (userId=? and beiUserId=?)";
        String sql = "select * from app_intimacy_guard where userId=? and beiUserId=?";
        list.add(userId);
        list.add(beiUserId);
//        list.add(beiUserId);
//        list.add(userId);
        return this.queryObj(sql,list,AppIntimacyGuard.class);
    }


    public AppIntimacyGuard addIntimacyGuard(AppIntimacyGuard appIntimacyGuard)
    {
        appIntimacyGuard.setId(this.add(appIntimacyGuard));
        return appIntimacyGuard;
    }



    public void incIntimacyGuard(Integer userId,Integer beiUserId,Double intimacy,Double giftMoney)
    {
        ArrayList<Object> list = new ArrayList<>();
//        String sql = " update app_intimacy_guard set intimacy=intimacy+? where (userId=? and beiUserId=?) or (userId=? and beiUserId=?)";
        String sql = " update app_intimacy_guard set intimacy=intimacy+?,giftMoney=giftMoney+? where userId=? and beiUserId=?";
        list.add(intimacy);
        list.add(giftMoney);
        list.add(userId);
        list.add(beiUserId);
//        list.add(beiUserId);
//        list.add(userId);
        this.update(sql,list);
    }


    public void updateGuard(Integer userId, Integer beiUserId, Integer isGuard)
    {
        ArrayList<Object> list = new ArrayList<>();
//        String sql = "update app_intimacy_guard set isGuard=1 where ((userId=? and beiUserId=?) or (userId=? and beiUserId=?)) and isGuard=2";
        String sql = "update app_intimacy_guard set isGuard=? where userId=? and beiUserId=? and isGuard=2";
        list.add(isGuard);
        list.add(userId);
        list.add(beiUserId);
//        list.add(beiUserId);
//        list.add(userId);
        this.update(sql,list);
    }

    public void updateGuardById(Integer id, Integer isGuard)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "update app_intimacy_guard set isGuard=? where id=? and isGuard=2";
        list.add(isGuard);
        list.add(id);
        this.update(sql,list);
    }


    /**
     * 获取当前守护的用户
     * @param beiUserId
     * @return
     */
    public AppIntimacyGuard getNowGuard(Integer beiUserId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from app_intimacy_guard where beiUserId=? and isGuard=1";
        list.add(beiUserId);
        return this.queryObj(sql,list,AppIntimacyGuard.class);
    }

    public AppIntimacyGuard getIntimacySum(Integer userId,Integer beiUserId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select *,sum(intimacy) from app_intimacy_guard where userId=? and beiUserID=?";
        list.add(userId);
        list.add(beiUserId);
        return this.queryObj(sql,list,AppIntimacyGuard.class);
    }

    /**
     * 查询指定小时数的亲密度记录
     * @param hours
     * @return
     */
    public List<AppIntimacyGuard> getHoursIntimacyList(BigDecimal hours,Integer userId,Integer beiUserId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "SELECT * FROM app_intimacy_guard WHERE userId=? and beiUserId=? and createtime>NOW()-INTERVAL ? HOUR";
        list.add(userId);
        list.add(beiUserId);
        list.add(hours);
        return this.queryList(sql,list,AppIntimacyGuard.class);
    }




    /**
     * 日排行
     * @return
     */
    public List<AppIntimacyGuard> getDayRanking()
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select *,sum(intimacy) as totalIntimacy from app_intimacy_guard where to_days(createtime) = to_days(now()) GROUP BY userId ORDER BY totalIntimacy desc limit 100";
        return this.queryList(sql,list,AppIntimacyGuard.class);
    }

    /**
     * 周排行
     * @return
     */
    public List<AppIntimacyGuard> getWeekRanking()
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select *,sum(intimacy) as totalIntimacy from app_intimacy_guard where YEARWEEK( date_format(  createtime,'%Y-%m-%d' ) ) = YEARWEEK( now() ) GROUP BY userId ORDER BY totalIntimacy desc limit 100";
        return this.queryList(sql,list,AppIntimacyGuard.class);
    }

    /**
     * 月排行
     * @return
     */
    public List<AppIntimacyGuard> getMonthRanking()
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select *,sum(intimacy) as totalIntimacy from app_intimacy_guard where DATE_FORMAT( createtime, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) GROUP BY userId ORDER BY totalIntimacy desc limit 100";
        return this.queryList(sql,list,AppIntimacyGuard.class);
    }

    public void delByUserId(Integer userId) {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "delete from app_intimacy_guard where userId = ? or beiUserId = ?";
        list.add(userId);
        list.add(userId);
        this.del(sql, list);
    }






}
